package com.zmx.patientend.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.DoctorItemAdapter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.Doctor;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*时间：2020/4/30 17:26
*功能：医生收藏
*/
public class CollectionDoctorActivity extends BaseActivity {

    private RecyclerView re_doctor;
    private DoctorItemAdapter doctorItemAdapter;
    private List<Doctor> doctors;
    private RefreshLayout mRefreshLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collection_doctor;
    }

    @Override
    protected void initViews() {
       setToolbar(R.id.tool_bar);

        re_doctor = findViewById(R.id.re_doctor);
        doctorItemAdapter = new DoctorItemAdapter(this);
        re_doctor.setLayoutManager(new LinearLayoutManager(this));
        re_doctor.setAdapter(doctorItemAdapter);
        doctors = new ArrayList<>();
        doctorItemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                String did = doctors.get(listPosition).getMember_id()+"";
                //目前Activity→目标Activity
                Intent intent = new Intent(mActivity,DoctorDetailsActivity.class);
                intent.putExtra("doctor_id",did);
                intent.putExtra("class_id",doctors.get(listPosition).getClass_id()+"");
                intent.putExtra("hosp_id",doctors.get(listPosition).getHosp_id()+"");
                startActivity(intent);

            }
        });
        // 开启自动加载功能
        mRefreshLayout = findViewById(R.id.refreshLayout);
        //触发自动刷新
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableAutoLoadMore(true);
        mRefreshLayout.setOnRefreshListener(refreshLayout -> {

            getFollowList();

        });

        mRefreshLayout.setOnLoadMoreListener(refreshLayout -> {

            //刷新加载
            mRefreshLayout.finishLoadMore(true);

        });


    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    doctors.clear();

                    Log.e("内容",""+msg.obj.toString());
                    JSONObject jsonObject = null;

                    try {
                        jsonObject = new JSONObject(msg.obj.toString());
                        JSONArray array = jsonObject.getJSONArray("data");

                        Gson g = new Gson();
                        for (int i = 0; i < array.length(); i++) {

                            JSONObject object = array.getJSONObject(i);
                            Doctor c = g.fromJson(object.toString(), Doctor.class);
                            doctors.add(c);

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    doctorItemAdapter.setListAndNotifyDataSetChanged(doctors);//就是设置list然后刷新
                    //刷新加载
                    mRefreshLayout.finishRefresh(true);
                    break;

            }

        }
    };

    public void getFollowList(){

        List<String> l = new ArrayList<>();
        l.add("10");
        l.add("1");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getFollowList");
        params.put("data[row]", "1");
        params.put("data[number]", "10");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/app/", params, handler, 1, 404);

    }

}
